#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver;
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re;
from pyquery import PyQuery as pq;
from lxml import etree
import pymongo;
from  config import *
# browser=webdriver.PhantomJS(service_args=SERVICE_ARGS);
browser=webdriver.Chrome()
# browser.set_window_size(1400,900)
wait=WebDriverWait(browser, 10);
client=pymongo.MongoClient(MONGO_URL);
db=client["163music"];
from bs4 import BeautifulSoup
import time
import requests
import pandas as pd
import requests
'''
浏览器滑到指定的元素页面
page_bu = browser.find_element_by_partial_link_text(u'下一页') 
browser.execute_script("arguments[0].scrollIntoView(false);",page_bu)
'''
def search():
    try:
        browser.get("http://music.163.com/#/song?id=29539350")
        browser.switch_to_frame("contentFrame")
        input = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, ".cmmts"))
        )
        get_comments()
    except TimeoutException:
        print("search timeout---------------------")
        print(browser.page_source)
        return search();


def next_page(page):
    try:
        print("current page",page)
        page_bu = browser.find_element_by_partial_link_text(u'下一页')
        browser.execute_script("arguments[0].scrollIntoView(false);", page_bu)
        time.sleep(2)
        click = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, u'下一页')))
        click.click()
        wait.until(
            EC.text_to_be_present_in_element(
                (By.CSS_SELECTOR, '.js-selected'), str(page)
            )
        )
        get_comments()
    except TimeoutException:
        print("next page timeout");
        next_page(page);


def get_comments():
    input = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".cmmts .itm"))
    )
    html=browser.page_source;
    doc=pq(html);
    items=doc(".m-cmmt .cmmts .itm").items();
    for item in items:
        item.find(".cntwrap .rp .s-fc3").remove()
        music={
            "user_name":item.find(".cntwrap .cnt.f-brk a").text(),
            "user_url":"http://music.163.com/#"+item.find(".cntwrap .cnt.f-brk a").attr("href"),
            "user_comment":item.find(".cntwrap .cnt.f-brk").text(),
            "timestamps":item.find(".cntwrap .rp .time").text(),
            "zan_count":item.find(".cntwrap .rp a").text()
        }
        print(music)
        saveToMongo(music)

def get_data():
    queryArgs = {}
    projectionFields = {'_id': True,"user_url":True,"zan_count":True}  # 用字典指定
    result = db["fengjuzhu"].find(queryArgs,projectionFields)
    return result

def main():
    search()
    for page in range(2,176):
        next_page(page)

def update_user():
    data=pd.DataFrame(list(get_data()))
    urls=list(data["user_url"])
    count=0
    for url in urls:
        count+=1
        print(count)
        get_fans(url)






def get_fans(url):
    count=0
    try:
        browser.get(url)
        browser.switch_to_frame("contentFrame")
        wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#fan_count"))
        )
        html = browser.page_source;
        doc = pq(html);
        count= doc("#fan_count").text()
    except:
        print("get fan counnt error")
    updateToMongo(url,count)


def updateToMongo(url, count):
    try:
        db["fengjuzhu"].update({"user_url": url}, {"$set": {"fan_count":count}})
        print("update success")
    except Exception as e:
        print(e)
        print("update error")



def saveToMongo(result):
    try:
        if db["fengjuzhu"].insert(result):
            print("save result success");
    except:
        print("save mongo error")


def requests_fans():
    headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
    res=requests.get("http://music.163.com/#/user/home?id=34827415",headers=headers)
    print(res.text)


if __name__ == '__main__':
    # main()
    requests_fans()



